//
//  NewsData.m
//  BosidengIphone
//
//  Created by  on 11-12-27.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "NewsData.h"
#import "AppDelegate.h"

@implementation NewsData

@synthesize infoID;
@synthesize publishDate;
@synthesize iPhoneSLImg;
@synthesize title;
@synthesize content;
@synthesize newsImgs;
@synthesize imageHeight;
@synthesize modifyDate;

- (id)init {
    if (self = [super init]) {
        ;
    }
    return self;
}

- (void)dealloc {
    [imageHeight release];
    [modifyDate release];
    [infoID release];
    [publishDate release];
    [iPhoneSLImg release];
    [title release];
    [content release];
    [newsImgs release];
    [super dealloc];
}

+ (NSString *)getAllItemsQueryString {
    NSString *query = [NSString stringWithFormat:@"WHERE language='%@' ORDER BY modify_date DESC", [AppDelegate getLanguage]];
    return query;
}

+ (id)getItemByID:(NSString *)uid {
    return [NewsData findFirstByCriteria:[NewsData getItemQueryString:@"info_i_d" value:uid]];
}

+ (NSArray *)getAllItems {
    return [NewsData findByCriteria:[NewsData getAllItemsQueryString]];
}

- (NSString *)description {
    return [NSString stringWithFormat:@"News Item.%d, infoID=%@", [self pk], infoID];
}

- (void)setValue:(NSDictionary *)data {
    [super setValue:data];
    self.infoID = [data objectForKey:@"InfoID"];
    self.publishDate = [data objectForKey:@"PublishDate"];
    self.iPhoneSLImg = [self formatStringData:[data objectForKey:@"iPhoneSLImg"]];
    self.title = [data objectForKey:@"Title"];
    self.content = [data objectForKey:@"Content"];
    self.newsImgs = [self formatStringData:[data objectForKey:@"NewsImgs"]];
    self.imageHeight = [self formatNumberData:[data objectForKey:@"iPhoneSLImgHeight"]];
    self.modifyDate = [data objectForKey:@"ModifyDate"];
}

- (void)removeSmallImage {
    NSString *imagePath = [AppDelegate getCacheFilePathByUrl:iPhoneSLImg];
    [AppDelegate deleteFile:imagePath];
}

- (void)removeNewsImages {
    NSArray *images = [newsImgs componentsSeparatedByString: @"|"];
    for (NSString *url in images) {
        NSString *imagePath = [AppDelegate getCacheFilePathByUrl:url];
        [AppDelegate deleteFile:imagePath];
    }
}

- (void)removeLocalImages {
    //删除本地数据包括图片
    if ([self hasSLImage]) {
        [self removeSmallImage];
    }
    if ([self hasNewsImgs]) {
        [self removeNewsImages];
    }
}

- (BOOL)hasSLImage {
    return ((iPhoneSLImg != nil) && ![@" " isEqualToString:iPhoneSLImg]);
}

- (BOOL)hasNewsImgs {
    return ((newsImgs != nil) && ![@" " isEqualToString:newsImgs]);
}

- (NSInteger)getContentHeight {
    return [self getTextHeight:content cx:300 font:[UIFont systemFontOfSize:10]];
}

- (NSInteger)getImageHeight {
    NSInteger height = [imageHeight intValue];
    UIImage *image = [noSaveData objectForKey:iPhoneSLImg];
    if (image == nil) {
        NSString *imagePath = [AppDelegate getCacheFilePathByUrl:iPhoneSLImg];
        if ([AppDelegate checkFileExist:imagePath]) {
            image = [UIImage imageWithContentsOfFile:imagePath];
            [noSaveData setValue:image forKey:iPhoneSLImg];
        }
    }
    if (image != nil) {
        height = image.size.height;
    }
    //NSLog(@"getImageHeight=%d", height);
    return height;
}

- (CGFloat)getHeight {
    int height = 0;//上间距
    height += 24;
    if ([self hasSLImage]) {
        height += [self getImageHeight]/2;
    }
    height += 22;
    height += [self getContentHeight];
    if ([self hasNewsImgs]) {
        height += 18;
    }
    height += 25;//下间距
    return height;
}


@end
